package com.ply.flashsalessystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ply.flashsalessystem.annotation.AuthorityName;
import com.ply.flashsalessystem.entity.pojo.*;
import com.ply.flashsalessystem.entity.result.Result;
import com.ply.flashsalessystem.entity.wrapper.ManageWrapper;
import com.ply.flashsalessystem.mapper.ManageMapper;
import com.ply.flashsalessystem.service.*;
import com.ply.flashsalessystem.utils.RuleUtils;
import com.ply.flashsalessystem.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XiaoPan
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/manage")
@Api("管理员接口")
@CrossOrigin
public class ManageController {
    @Autowired
    GoodsService goodsService;

    @Autowired
    OrderForGoodsService orderForGoodsService;

    @Autowired
    OrderForGoodsDetaileService orderForGoodsDetaileService;

    @Autowired
    TagService tagService;

    @Autowired
    JionRepositoryService jionRepositoryService;

    @Autowired
    JionRepositoryDetailsService jionRepositoryDetailsService;

    @Autowired
    UserService userService;

    @Autowired
    SortService sortService;

    @Autowired
    StoreService storeService;

    @Autowired
    ManageService manageService;

    @Autowired
    ManageMapper manageMapper;

    @Autowired
    AuthorityService authorityService;


    /*
    商家
     */
//    @AuthorityName(authorityId = 12, authorityName = "查询所有商家")
//    @ApiOperation("查询所有商家")
//    @GetMapping("getStoreAll")
//    public Result getStoreAll() {
//        List<Store> list = storeService.list();
//        return Result.ok().message("查询成功").data("rows", list);
//    }

    /*
    管理员管理  查看所有管理员    增加管理员   (权限controller )修改权限    增加权限
     */

    /*
    管理员的管理 增加在loginController 中写好了  这边只用写查询 所有管理员
     */
    @AuthorityName(authorityId = 13, authorityName = "分页条件查询所有管理员")
    @ApiOperation("分页条件查询所有管理员")
    @PostMapping(value = {"getAdminAll/{current}/{limit}", "getAdminAll"})
    public Result getAdminAll(@PathVariable(value = "current", required = false) Integer current,
                              @PathVariable(value = "limit", required = false) Integer limit,
                              @RequestBody(required = false) ManageWrapper manageWrapper) {
        if (current == null || limit == null) {
            current = 0;
            limit = 10;
        }
        QueryWrapper<Manage> queryWrapper = null;
        if (manageWrapper == null) {
            manageWrapper = new ManageWrapper();
        }else {
            queryWrapper = new QueryWrapper<>();
            queryWrapper.like("name", manageWrapper.getName()).orderByDesc("create_time");
        }

        Page<Manage> page = manageService.page(new Page<>(current, limit), queryWrapper);
        return Result.ok().message("查询成功").data("total", page.getTotal()).data("rows", page.getRecords());
    }


    @AuthorityName(authorityId = 14, authorityName = "查看自己管理员的信息")
    @ApiOperation("查看自己管理员的信息")
    @PostMapping("getAdminInfo")
    public Result getAdminInfo() {
        Manage byId = manageService.getById(UserUtils.getUserId());
//        manageMapper.getById(UserUtils.getUserId());
        byId.setPwd(null);
        return Result.ok().message("查询成功").data("info", byId);
    }

    /**
     * 删除管理员 逻辑删除
     */
    @AuthorityName(authorityId = 15, authorityName = "逻辑删除 管理员")
    @ApiOperation("逻辑删除 管理员")
    @DeleteMapping("deleteManage/{manageId}")
    public Result deleteManage(@PathVariable("manageId") Integer manageId) {
        if (manageService.getById(manageId) == null){
            throw new RuntimeException("管理员id 错误");
        }
        manageService.deleteManage(manageId);
        return Result.ok().message("删除成功");
    }

    @AuthorityName(authorityId = 16, authorityName = "管理员注册")
    @ApiOperation("管理员注册")
    @PostMapping("addManage")
    @Transactional
    public Result addManage(@RequestBody Manage manage1) {
        String name = manage1.getName();
        String pwd = manage1.getPwd();

        if (!RuleUtils.userName(name)) {
            return Result.error().message(RuleUtils.nameError);
        }

        if (!RuleUtils.pwdPattern(pwd)) {
            return Result.error().message(RuleUtils.pwdError);
        }
        //判断用户名 是否存在
        QueryWrapper<Manage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        Manage one = manageService.getOne(queryWrapper);
        //可以注册
        if (one != null){
            return Result.error().message("name 重复");
        }

        Manage manage = new Manage();
        manage.setName(name);
        manage.setPwd(pwd);
        if (manageService.save(manage)){
            // 注册成功 给 管理员 增加 权限组
            authorityService.addAllGroup(manage.getManageId());
            return Result.ok().message("管理员注册成功");
        }
        return Result.error().message("管理员注册失败");
    }
}

